//
//  DHUserAddressesController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/24.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHUserAddressesController.h"
#import "DHUserAddressCell.h"
#import "DHContactViewController.h"
#import "Address.h"

@interface DHUserAddressesController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSString *address_id;

@end

@implementation DHUserAddressesController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"常用地址";
    
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(self.navView.width - 54, self.navView.height - 44, 44, 44)];
    [addButton setTitle:@"新增" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [addButton addTarget:self action:@selector(addAddress) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:addButton];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHUserAddressCell" bundle:nil] forCellReuseIdentifier:kDHUserAddressCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    [self loadData];
    
    addNObserver(@selector(addAddress:), @"DHUserAddressesController");
}

- (void)dealloc{
    removeNObserver(@"DHUserAddressesController");
}

- (void)addAddress:(NSNotification *)not{
    if (not){
        NSDictionary *dic = not.object;
        Address *address = [dic objectForKey:@"address"];
        Location *location = [dic objectForKey:@"location"];

        NSDictionary *params = @{@"label":address.label,
                                 @"contact":address.name,
                                 @"province":address.province,
                                 @"city":address.city,
                                 @"district":address.district,
                                 @"address":address.address,
                                 @"accurate":address.accurate,
                                 @"phone":address.phone,
                                 @"alternate":address.phone,
                                 @"location":@{@"coordinates":location.coordinates}
                                 };
        if (self.address_id) {
            NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:params];
            [temp setObject:self.address_id forKey:@"address_id"];
            [[DHNetAPIClient shareDHNetAPIClient] request_user_address_modifyWithParams:temp andBlock:^(id data, NSError *error) {
                [NSObject showHudTipStr:@"修改成功"];
                [self loadData];
            }];
        }else{
            [[DHNetAPIClient shareDHNetAPIClient] request_user_address_createWithParams:params andBlock:^(id data, NSError *error) {
                [NSObject showHudTipStr:@"添加成功"];
                [self loadData];
            }];
        }
    }
}

- (void)loadData{
    [[DHNetAPIClient shareDHNetAPIClient] request_user_addressAndBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        self.arr = [data objectForKey:@"addresses"];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)addAddress{
    self.address_id = nil;
    DHContactViewController *vc = [[DHContactViewController alloc] init];
    vc.isManager = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHUserAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHUserAddressCell forIndexPath:indexPath];
    NSDictionary *address = [self.arr objectAtIndex:indexPath.row];
    [cell updateCell:address];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    Address *address = [[Address alloc] init];
    address.label = [dic objectForKey:@"label"];
    address.accurate = [dic objectForKey:@"accurate"];
    address.address = [dic objectForKey:@"address"];
    address.city = [dic objectForKey:@"city"];
    address.phone = [dic objectForKey:@"phone"];
    address.district = [dic objectForKey:@"district"];
    address.province = [dic objectForKey:@"province"];
    address.name = [dic objectForKey:@"contact"];
    
    self.address_id = [dic objectForKey:@"_id"];
    DHContactViewController *vc = [[DHContactViewController alloc] init];
    vc.isManager = YES;
    vc.address = address;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
        [[DHNetAPIClient shareDHNetAPIClient] request_user_address_deleteWithParams:@{@"address_id":[dic objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
            [self loadData];
        }];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
